﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using UnityEngine.AI;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    public class EnvironmentUnit: BattleUnit
    {
        private int nextStateIndex;
        private List<MapObstacleState> states;
        private List<bool> objectValid;
        public Collider collider;
        //死亡效果
        public int bulletId;
        public ETargetTeam targetTeam;
        private Transform origin;

        private HpStateDisplay hpStateDisplay;
        private bool hasHpStateDisplay;

        private MapObstacleData _obstacleData;

        //private NavMeshObstacle navMeshObstacle;
        
        public void Init(MapObstacleData obstacleData)
        {
            _obstacleData = obstacleData;
            currentHp = obstacleData.hp;
            
            totalHp = obstacleData.hp;
            attack = obstacleData.attack;
            defence = obstacleData.defence;
            hpStateDisplay = obstacleData.gameObject.GetComponent<HpStateDisplay>();
            hasHpStateDisplay = hpStateDisplay != null;
            hpStateDisplay?.Init();
            if (!hasHpStateDisplay)
            {
                states = obstacleData.states;
                objectValid = new List<bool>();
                for (int i = 0; i < states.Count; i++)
                {
                    objectValid.Add(states[i].stateObject != null);
                }
            }
            bulletId = obstacleData.bulletId;
            targetTeam = obstacleData.targetTeam;
            position = obstacleData.transform.position;
            forward = obstacleData.transform.forward;
            origin = obstacleData.transform;
            AimPoint = obstacleData.transform;
            var ratio = StageManager.Instance.missionConfig.attribute_ratio;
            var attackRatio = StageManager.Instance.missionConfig.attribute_ratio_atk * ratio;
            var defRatio = StageManager.Instance.missionConfig.attribute_ratio_def * ratio;
            var hpRatio = StageManager.Instance.missionConfig.attribute_ratio_hp * ratio;
            totalHp = currentHp = (int)(currentHp * hpRatio);
            attack = (int)(attack * attackRatio);
            defence = (int)(defence * defRatio);
            OnHpChanged();
            //navMeshObstacle = obstacleData.gameObject.GetComponentInChildren<NavMeshObstacle>();
        }

        public void SetCollider(Collider collider)
        {
            this.collider = collider;
            if (collider is BoxCollider boxCollider)
            {
                collisionRadius = boxCollider.size.x * 0.5f;
                AddComponent<CollisionComponent>();
            }
            else
            {
                BattleDebug.LogError("不支持的碰撞器 " + collider.GetType().Name);
            }
            collider.enabled = true;
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            position = origin.position;
            forward = origin.forward;
        }

        public override void InitHero()
        {
            
        }

        public override void InitComponents()
        {
            
        }
        
        protected override bool CheckCanAddElement(ElementConfig elementConfig)
        {
            return elementConfig.main_type == (int)EBattleElementMainType.Damage;
        }

        protected override void OnDead()
        {
            base.OnDead();
            if (collider)
            {
                collider.enabled = false;
            }
            CreateDeathBullet();
            /*if (navMeshObstacle != null)
            {
                navMeshObstacle.enabled = false;
            }*/
        }

        public void CreateDeathBullet()
        {
            if (bulletId > 0)
            {
                var config = BattleConfigManager.Instance.GetBulletConfig(bulletId);
                var bullet = BulletManager.Instance.CreateBullet(config);
                if (bullet != null)
                {
                    //BattleDebug.LogError("掩体爆炸出发子弹 " + bulletId + " " + position);
                    bullet.Initialize(config,StageManager.Instance.GetEnvironmentSpawner(targetTeam),this);
                    bullet.SetStartPosition(Position);
                    bullet.SetTargetPosition(Position);
                    bullet.Start();
                }
                else
                {
                    BattleDebug.LogError("掩体爆炸子弹找不到 " + bulletId + " " + position);
                }
            }
        }

        protected override void OnHpChanged()
        {
            var hpPercent = (float)currentHp / totalHp;
            if (hasHpStateDisplay)
            {
                hpStateDisplay.OnHpChange(hpPercent);
                return;
            }
#if UNITY_EDITOR
            BattleDebug.LogError("还在用老的生命控制，注意一下 " + _obstacleData.name);
#endif
            for (int i = 0; i < states.Count; i++)
            {
                var down = states[i].down;
                if (down <= 0)
                {
                    down = -0.1f;
                }
                if (hpPercent > down && hpPercent <= states[i].up)
                {
                    if (objectValid[i])
                    {
                        states[i].stateObject.SetActive(true);
                    }
                    
                }
                else
                {
                    if (objectValid[i])
                    {
                        states[i].stateObject.SetActive(false);
                    }
                }
            }
        }
    }
}
